﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AGR.Services.Employee;
using AGR.Services.Employee.Contract;
using AGR.Model;
using AGR.Services;

namespace AGR.Web.Controls.Form
{
    public partial class AddEmployee : System.Web.UI.UserControl
    {
        private IEmployeeService _employeeService = new EmployeeService();

        protected void Page_Load(object sender, EventArgs e)
        { }

        #region Public Methods

        public void LoadForm(string primaryKey)
        {
            var employee = _employeeService.GetEmployee(primaryKey);

            txtEmployeeCode.Text = primaryKey;
            txtHireDate.Text = employee.HireDate.ToString();
            txtFirstName.Text = employee.FirstName;
            txtLastName.Text = employee.LastName;
            txtPostalAddress.InnerText = employee.PostalAddress;
            txtPhysicalAddress.InnerText = employee.PhysicalAddress;
            rbtnMale.Checked = employee.Gender.Equals("M") ? true : rbtnFemale.Checked = true;
            ddlType.SelectedValue = employee.Type;
            txtIdNumber.Text = employee.IdNumber;
            chkIsTrainer.Checked = employee.IsTrainer == true ? true : false;
            lblTrainerSpecialization.Visible = chkIsTrainer.Checked;
            txtTrainerSpecialization.Visible = chkIsTrainer.Checked;
            txtTrainerSpecialization.Text = employee.TrainerSpecialization;

            txtEmployeeCode.Enabled = false;
            btnReset.Visible = true;
        }

        public void ClearForm()
        {
            txtEmployeeCode.Enabled = true;
            btnReset.Visible = false;
            this.Clear();
        }

        #endregion

        #region Event Methods

        protected void chkSameAddress_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSameAddress.Checked == true)
            {
                txtPostalAddress.InnerText = txtPhysicalAddress.InnerText;
            }
            else
            {
                txtPostalAddress.InnerText = "";
            }
        }

        protected void chkIsTrainer_CheckedChanged(object sender, EventArgs e)
        {
            if (chkIsTrainer.Checked == true)
            {
                lblTrainerSpecialization.Visible = true;
                txtTrainerSpecialization.Visible = true;
            }
            else
            {
                lblTrainerSpecialization.Visible = false;
                txtTrainerSpecialization.Visible = false;
            }
        }

        #endregion

        #region Button Methods

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ActionResult result = null;

            string code = txtEmployeeCode.Text;

            Nullable<DateTime> hireDate = BusinessHelper.GetDateTime(txtHireDate.Text);

            if (hireDate == null)
            {
                ucMessage.ShowMessage("Your action could not be completed. The hire date provided (" + txtHireDate.Text + ") is not in the correct date format (01 January 2013) or (2013/01/28).", Message.Message.MessageType.Warning);
                return;
            }

            // The code will be disabled if its an update/edit due to not being able to update the primary key of an existing entity.
            if (txtEmployeeCode.Enabled == true)
            {
                var checkExists = _employeeService.GetEmployee(code);

                if (checkExists != null)
                {
                    ucMessage.ShowMessage("Your action could not be completed. A record with employee code (" + code + ") already exists on the system.", Message.Message.MessageType.Warning);
                    return;
                }

                var entity = new Employee
                {
                    EmployeeCode = code,
                    FirstName = txtFirstName.Text,
                    LastName = txtLastName.Text,
                    PostalAddress = txtPostalAddress.InnerText,
                    PhysicalAddress = txtPhysicalAddress.InnerText,
                    Gender = rbtnMale.Checked == true ? "M" : "F",
                    Type = ddlType.SelectedValue,
                    IdNumber = txtIdNumber.Text,
                    HireDate = hireDate,
                    IsTrainer = chkIsTrainer.Checked,
                    TrainerSpecialization = txtTrainerSpecialization.Visible == true ? txtTrainerSpecialization.Text : string.Empty,
                    UserCreated = "TestUser"
                };

                result = _employeeService.AddEmployee(entity);
            }
            else
            {
                var entity = _employeeService.GetEmployee(code);

                entity.HireDate = hireDate;
                entity.FirstName = txtFirstName.Text;
                entity.LastName = txtLastName.Text;
                entity.PostalAddress = txtPostalAddress.InnerText;
                entity.PhysicalAddress = txtPhysicalAddress.InnerText;
                entity.Gender = rbtnMale.Checked == true ? "M" : "F";
                entity.Type = ddlType.SelectedValue;
                entity.IdNumber = txtIdNumber.Text;
                entity.IsTrainer = chkIsTrainer.Checked;
                entity.TrainerSpecialization = txtTrainerSpecialization.Visible == true ? txtTrainerSpecialization.Text : string.Empty;
                entity.UserModified = "TestUser";

                result = _employeeService.UpdateEmployee(entity);
            }

            if (result.Success)
                ucMessage.ShowMessage(result.Message, Message.Message.MessageType.Success);
            else
                ucMessage.ShowMessage(result.Message, Message.Message.MessageType.Error);
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.Clear();
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            this.LoadForm(txtEmployeeCode.Text);
        }

        #endregion

        #region Private Methods

        private void Clear()
        {
            ucMessage.ClearMessage();

            if (txtEmployeeCode.Enabled == true)
                txtEmployeeCode.Text = string.Empty;
            txtHireDate.Text = string.Empty;
            txtFirstName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtPostalAddress.InnerText = string.Empty;
            txtPhysicalAddress.InnerText = string.Empty;
            rbtnMale.Checked = true;
            ddlType.SelectedIndex = 0;
            txtIdNumber.Text = string.Empty;
            chkIsTrainer.Checked = false;
            lblTrainerSpecialization.Visible = false;
            txtTrainerSpecialization.Visible = false;
            txtTrainerSpecialization.Text = string.Empty;
        }

        #endregion
    }
}